package com.zzh.netty.byteBufDemo;
/**
 * @ClassName NettyByteBuf
 * @date 2021/2/1 20:34
 * @Version 1.0
 * @Description //TODO
 */

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;
import org.apache.tomcat.util.digester.DocumentProperties;

/**
 *@ClassName NettyByteBuf
 *@date 2021/2/1 20:34
 *@Version 1.0
 *@Description //
 */
public class NettyByteBuf {
    public static void main(String[] args) {
        ByteBuf byteBuf = Unpooled.buffer(10);
        System.out.println("byteBuf="+byteBuf);


        for (int i = 0; i < 8; i++) {
            byteBuf.writeByte(i);
        }
        System.out.println("byteBuf="+byteBuf);


        for (int i = 0; i < 5; i++) {
            System.out.println(byteBuf.getByte(i));
        }
        System.out.println("byteBuf="+byteBuf);


        for (int i = 0; i < 5; i++) {
            System.out.println(byteBuf.readByte());
        }
        System.out.println("byteBuf="+byteBuf);


        ByteBuf byteBuf1 = Unpooled.copiedBuffer("hello,zzh!", CharsetUtil.UTF_8);
        if (byteBuf1.hasArray()) {
            byte[] content = byteBuf1.array();
            System.out.println(new String(content,CharsetUtil.UTF_8));
            System.out.println("byteBuf="+byteBuf1);
            //获取当前位置的 字节码
            System.out.println(byteBuf1.getByte(0));
            int len = byteBuf1.readableBytes();
            System.out.println("可读字节  len = " + len);
            for (int i = 0; i < len; i++) {
                System.out.println((char) byteBuf1.getByte(i));
            }
            //读取范围
            System.out.println(byteBuf1.getCharSequence(0,6,CharsetUtil.UTF_8));
            System.out.println(byteBuf1.getCharSequence(6,6,CharsetUtil.UTF_8));
        }
    }
}
